package cn.bb.action;

import cn.bb.pojo.Person;
import cn.bb.service.IPersonService;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@Scope(value = "prototype")
@ParentPackage("struts-default")
public class IPersonAction extends ActionSupport {
    private Person person;
    @Autowired
    @Qualifier(value = "personService")
    private IPersonService iPersonService;

    @Action(value = "person_register", results = {
            @Result(name = "a1",location = "/success.jsp")
    })
    public String register(){
        System.out.println(person);
        iPersonService.register(person);
        return "a1";
    }

    @Action(value = "person_login", results = {
            @Result(name = "loginSUC",location = "/loginsuccess.jsp"),
            @Result(name = "loginFAIL",location = "/loginfail.jsp")
    })
    public String login(){
       Boolean boo =  iPersonService.login(person);
       if(boo){
           return "loginSUC";
       }else{
           return "loginFAIL";
       }
    }

    @Action(value = "person_list", results = {
            @Result(name = "success",location = "/list.jsp")
    })
    public String list(){
        List<Person> list = iPersonService.search();
        HttpServletRequest httpServletRequest = ServletActionContext.getRequest();
        httpServletRequest.setAttribute("list",list);
        return SUCCESS;
    }
    public Person getPerson() {
        return person;
    }

    public void setPerson(Person person) {
        this.person = person;
    }

    public IPersonService getiPersonService() {
        return iPersonService;
    }

    public void setiPersonService(IPersonService iPersonService) {
        this.iPersonService = iPersonService;
    }
}
